package com.unknowpupil.cloud.consumer.oauth.service;

import com.google.common.collect.Lists;
import com.unknowpupil.cloud.provider.account.api.IUnAccountService;
import com.unknowpupil.cloud.provider.account.domain.UnAccount;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Classname: UserDetailsServiceImpl
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/1/4 2:17
 * @Description:
 * @Version: 1.0
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Reference(version = "1.0.0")
    private IUnAccountService accountService;
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "$2a$10$bAwRXxUsbMz9rAG2hC2m8eDNh5LGnsVn5vPXtWWy0mE.ZoZVEl38q";

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //默认user权限
        //权限
        List<GrantedAuthority> grantedAuthorities = Lists.newArrayList();
        //这个是授权
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("USER");
        //模拟账号，
        grantedAuthorities.add(grantedAuthority);
        UnAccount account = accountService.get(username);
        //账号存在
        if (account != null) {
            return new User(account.getUserName(), account.getPassword(), grantedAuthorities);
        }
        //不存在
        else {
            return null;
        }

    }
}
